//
//  BHImageOrVideoModel.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/6.
//

import UIKit
import Photos

class BHImageOrVideoModel: NSObject {
    var sourceType: Int = 0 // 0图片  1视频
    
    var asset: PHAsset?
    var index: Int = 0
    var imageUrl: String?
    var videoUrl: String?
    var takeImage: UIImage?  {
        didSet {
            if takeImage != nil {
                self.highQualityFormatImage = takeImage!
            }
        }
    }// 如果asset是空这个就不为空
    
    
    func transAssetToImage(handler: ((UIImage?) -> Void)?) {
        if self.highQualityFormatImage != nil {
            handler?(self.highQualityFormatImage)
        }else {
            if self.asset != nil {
                PHCachingImageManager.default().requestImage(for: self.asset!, targetSize: .zero, contentMode: .aspectFill, options: nil) { (image, resultDic) in
                    self.highQualityFormatImage = image
                    handler?(image)
                }
            }else {
                if self.takeImage != nil {
                    handler?(self.takeImage)
                }else {
                    handler?(nil)
                }
            }
        }
    }
    
    var highQualityFormatImage: UIImage?
    
    
    lazy var videoAsset: AVAsset? = {
        var avAseet: AVAsset?
        PHCachingImageManager.init().requestAVAsset(forVideo: self.asset!, options: .none) { (avsset, audioMix, info) in
            avAseet = avsset
        }
        return avAseet
    }()
    
    
    
    func transToVideoData(handler: @escaping (Data?) -> Void) {
        if self.asset == nil{
            handler(nil)
        }
        PHCachingImageManager.init().requestAVAsset(forVideo: self.asset!, options: .none) { (avsset, audioMix, info) in
            if avsset == nil {
                handler(nil)
            }else {
                let urlAsset = avsset as? AVURLAsset
                if urlAsset == nil {
                    handler(nil)
                }else {
                    let data = try? Data(contentsOf: urlAsset!.url)
                    handler(data)
                }
            }
        }
    }
    
    
    lazy var durationTime: String = {
        if self.asset!.mediaType == .video {
            
            let du = Int(self.asset!.duration)
            let min = du/60
            let sec = du%60
            
            let time = String.init(format: "%.2d:%.2d", min,sec)
            
            return time
        }
        return "00:00"
    }()
    
    
    
    static func transeToImage(array: [BHImageOrVideoModel]) -> [UIImage] {
        var temp: [UIImage] = []
        
        for model in array {
            if model.highQualityFormatImage == nil {
                if model.takeImage != nil {
                    temp.append(model.takeImage!)
                }
            }else {
                temp.append(model.highQualityFormatImage!)
            }
        }
        return temp
    }
    
    static func transeToImageUrl(array: [BHImageOrVideoModel]) -> [String] {
        var temp: [String] = []
        
        for model in array {
            if model.imageUrl != nil {
                temp.append(model.imageUrl!)
            }
        }
        return temp
    }
}
